package com.lpy.aop.aspect;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class AspectDemo {

//    定义切点
    @Pointcut("execution(* com.lpy.aop.controller.*.*(..))")
    public void pt() {};

//    注解的方式
    @Before("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void doBefore() {
        log.info("执行 before 方法");
    }

    @After("pt()")
    public void doAfter() {
        log.info("执行 After 方法");
    }

    @AfterReturning("pt()")
    public void doAfterReturn() {
        log.info("执行 AfterReturning 方法");
    }

    @AfterThrowing("pt()")
    public void doAfterThrowing() {
        log.info("执行 AfterThrowing 方法");
    }

    @Around("pt()")
    public Object doAround(ProceedingJoinPoint pj) {
        log.info("执行 Around, 目标方法前");

        Object object = null;
        try {
            object = pj.proceed();
        } catch (Throwable e) {
            log.error(pj.toShortString() + "发生异常, e:" + e);
        }


        log.info("执行 Around, 目标方法后");

        return object;
    }

}
